home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Text / Text Editors / Alpha 5.31 Folder / Tcl / SystemCode / electricC.tcl < prev    next >
Encoding:
Text File  |  1993-01-30  |  3.6 KB  |  166 lines  |  [TEXT/ALFA]

  1. #=============================================================================
  2. # "Electric" C functions.
  3. #=============================================================================
  4.  
  5. # First, define macros to bypass the electric braces.
  6. proc ordLeftBrace {} {
  7.     insertText "\{"
  8. }
  9. bind {'['} <cs> ordLeftBrace
  10.  
  11. proc ordRightBrace {} {
  12.     insertText "\}"
  13.     blink [matchIt "\}" [expr [getPos]-1]]
  14. }
  15. bind {']'} <cs> ordRightBrace
  16.     
  17. proc quoteWord {} {
  18.     backwardWord
  19.     insertText "'"
  20.     forwardWord
  21.     insertText "'"
  22. }
  23. bind ''' <z> quoteWord
  24.  
  25. # returns the indent string of the line named by 'pos'
  26. proc indentString pos {
  27.     set start [lineStart $pos]
  28.     set end [nextLineStart $pos]
  29.     set text [getText $start $end]
  30.     for {set i 0} {1} {incr i} {
  31.         set c [string index $text $i]
  32.         if {($c != "\ ") && ($c != "\t")} then {
  33.             return [string range $text 0 [expr $i-1]]
  34.         }
  35.     }
  36.     return
  37. }
  38.  
  39.  
  40. # Brace on new line, same indentation. Insert on another new line, indented in.
  41. # First, see if we are on new line.
  42. proc electricCLeft {} {
  43.     global elecLBrace
  44.     deleteText [getPos] [selEnd]
  45.     if {$elecLBrace == "0"} then {
  46.         insertText "\{"
  47.         return
  48.     }
  49.     set pos [getPos]
  50.     set start [lineStart $pos]
  51.     set text [getText $start $pos]
  52.     
  53.     for {set i $start} {$i < $pos} {incr i} {
  54.         set c [lookAt $i]
  55.         if {($c != "\ ") && ($c != "\t")} then {
  56.             set indentation [getText $start $i]
  57.             insertText "\r" $indentation "\{\r" $indentation "\t"
  58.             return
  59.         }
  60.     }
  61.     set indentation [getText $start $pos]
  62.     insertText "\{\r" $indentation "\t"
  63. }
  64. bind '\{' <s> electricCLeft
  65.  
  66.  
  67. # Brace on new line, immediate carriage return
  68. proc electricCRight {} {
  69.     global elecRBrace
  70.     deleteText [getPos] [selEnd]
  71.     if {$elecRBrace == "0"} then {
  72.         insertText "\}"
  73.         catch {blink [matchIt "\}" [expr [getPos]-2]]}
  74.         return
  75.     }
  76.     set pos [getPos]
  77.     set start [lineStart $pos]
  78.     
  79.     if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
  80.         beep
  81.         return
  82.     }
  83.     set text [getText [lineStart $matched] $matched]
  84.     regexp {^[     ]*} $text indentation
  85.     for {set i $start} {$i < $pos} {incr i} {
  86.         set c [lookAt $i]
  87.         if {($c != "\ ") && ($c != "\t")} then {
  88.             insertText "\r" $indentation "\}\r" $indentation
  89.             blink $matched
  90.             return
  91.         }
  92.     }
  93.     set text [set indentation]\}\r$indentation
  94.     replaceText $start $pos $text
  95.     goto [expr {$start + [string length $text]}]
  96.     blink [matchIt "\}" [expr $start-2]]
  97. }
  98. bind '\}' <s> electricCRight
  99.  
  100.  
  101. # Brace on new line, immediate carriage return. We don't do our electric stuff
  102. # if we are in the middle of a for statement.
  103. proc electricCSemi {} {
  104.     global electricSemi
  105.     deleteText [getPos] [selEnd]
  106.     if {$electricSemi == "0"} then {
  107.         insertText ";"
  108.         return
  109.     }
  110.     set pos [getPos]
  111.     set start [lineStart $pos]
  112.     set text [getText $start $pos]
  113.     
  114.     if {[string first "for" $text] != "-1"} {
  115.         set lefts 0
  116.         set rights 0
  117.         set len [string length $text]
  118.         for {set i 0} {$i < $len} {incr i} {
  119.             case [string index $text $i] in {
  120.                 "("    { incr lefts }
  121.                 ")"    { incr rights }
  122.             }
  123.         }
  124.         global globs
  125.         set globs [list $lefts $rights $len]
  126.         if {$lefts != $rights} {
  127.             insertText ";"
  128.             return
  129.         }
  130.     }
  131.     
  132.     insertText ";\r" [indentString $pos]
  133. }
  134. bind '\;' electricCSemi
  135.  
  136.  
  137. # 'C' programming mode 
  138. proc setCMode {} {
  139.     changeMode "C"
  140.     uplevel #0 {
  141.         set elecLBrace 1
  142.         set elecRBrace 1
  143.         set electricSemi 1
  144.         set wordWrap 0
  145.         set funcExpr {^[^ \t\(#\r/@].*\(.*\)$}
  146.         set sortedIsDefault 1
  147.         set funcTitle "Func"
  148.     }
  149. }
  150.  
  151. proc setC++Mode {} {
  152.     changeMode "C++"
  153.      uplevel #0 {
  154.         set elecLBrace 1
  155.         set elecRBrace 1
  156.         set electricSemi 1
  157.         set wordWrap 0
  158.           set funcExpr {^([^ \t\(#\r/@].*[ \t]+)?([A-Za-z0-9:~_]+)[ \t\r]*\(.*\)?$}
  159.         set funcPar 2
  160.         set funcTitle "Meth"
  161.         set sortedIsDefault 1
  162.     }
  163. }
  164.  
  165.  
  166.